<template>
  <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
    <el-form ref="form" :model="form" :disabled="flag==1" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="属性名称" prop="attributeName" required>
            <el-input v-model="form.attributeName" placeholder="请输入" maxlength="20"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="属性编码" prop="attributeKey" required>
            <el-input v-model="form.attributeKey" placeholder="请输入" maxlength="20"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="属性类型" prop="attributeType" required>
            <el-select v-model="form.attributeType" filterable style="width: 100%">
              <el-option
                v-for="item in dict.type.sys_attr_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="是否必填" prop="isMust" required>
            <el-select v-model="form.isMust" style="width: 100%">
              <el-option
                v-for="item in dict.type.sys_yes_no"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="定价元素" prop="isPrice" required>
            <el-select v-model="form.isPrice" clearable style="width: 100%">
              <el-option
                v-for="item in dict.type.sys_yes_no"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="属性单位" prop="unit">
            <el-input v-model="form.unit" placeholder="请输入" maxlength="30"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="显示排序" prop="orderNum" required>
            <el-input type="number" v-model="form.orderNum" placeholder="请输入" :min="0" maxlength="30"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="form.attributeType==1">
      <el-button type="primary" @click="list.push({})">新增</el-button>
      <el-button @click="list=[]">清空</el-button>
      <el-table
        :data="list"
        max-height="300"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="value" label="属性值" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.value" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible=false">取 消</el-button>
      <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
import {addGoodAttr, updateGoodAttr} from "../../../api/goodMng";

export default {
  dicts: ['sys_yes_no', 'sys_attr_type'],
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      list: [],
      form: {},
      titleList: ['新增', '详情', '修改']
    }
  },
  methods: {
    show(flag, form) {
      this.dialogVisible = true
      this.flag = flag
      this.list = []
      this.resetForm('form')
      this.form = form
      if (flag) {
        this.list = form.skuValueList
      }
    },
    deleteRow(row) {
      this.list = this.list.filter(val => val != row)
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.skuValueList = []
          if (this.form.attributeType == 1) {
            if (this.list && this.list.length) {
              this.form.skuValueList = this.list
            } else {
              this.$message.error('属性值不能为空')
              return
            }
          }
          this.loading = true
          if (this.flag) {
            updateGoodAttr(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            addGoodAttr(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
